<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>asynctestcase.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_asynctestcase.js.html">asynctestcase.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>// All Rights Reserved.
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview A class representing a set of test functions that use
<a name="line18"></a> * asynchronous functions that cannot be meaningfully mocked.
<a name="line19"></a> *
<a name="line20"></a> * To create a Google-compatable JsUnit test using this test case, put the
<a name="line21"></a> * following snippet in your test:
<a name="line22"></a> *
<a name="line23"></a> *   var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line24"></a> *
<a name="line25"></a> * To make the test runner wait for your asynchronous behaviour, use:
<a name="line26"></a> *
<a name="line27"></a> *   asyncTestCase.waitForAsync(&#39;Waiting for xhr to respond&#39;);
<a name="line28"></a> *
<a name="line29"></a> * The next test will not start until the following call is made, or a
<a name="line30"></a> * timeout occurs:
<a name="line31"></a> *
<a name="line32"></a> *   asyncTestCase.continueTesting();
<a name="line33"></a> *
<a name="line34"></a> * There does NOT need to be a 1:1 mapping of waitForAsync calls and
<a name="line35"></a> * continueTesting calls. The next test will be run after a single call to
<a name="line36"></a> * continueTesting is made, as long as there is no subsequent call to
<a name="line37"></a> * waitForAsync in the same thread.
<a name="line38"></a> *
<a name="line39"></a> * Example:
<a name="line40"></a> *   // Returning here would cause the next test to be run.
<a name="line41"></a> *   asyncTestCase.waitForAsync(&#39;description 1&#39;);
<a name="line42"></a> *   // Returning here would *not* cause the next test to be run.
<a name="line43"></a> *   // Only effect of additional waitForAsync() calls is an updated
<a name="line44"></a> *   // description in the case of a timeout.
<a name="line45"></a> *   asyncTestCase.waitForAsync(&#39;updated description&#39;);
<a name="line46"></a> *   asyncTestCase.continueTesting();
<a name="line47"></a> *   // Returning here would cause the next test to be run.
<a name="line48"></a> *   asyncTestCase.waitForAsync(&#39;just kidding, still running.&#39;);
<a name="line49"></a> *   // Returning here would *not* cause the next test to be run.
<a name="line50"></a> *
<a name="line51"></a> * This class supports asynchronous behaviour in all test functions except for
<a name="line52"></a> * tearDownPage. If such support is needed, it can be added.
<a name="line53"></a> *
<a name="line54"></a> * Example Usage:
<a name="line55"></a> *
<a name="line56"></a> *   var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line57"></a> *   // Optionally, set a longer-than-normal step timeout.
<a name="line58"></a> *   asyncTestCase.stepTimeout = 30 * 1000;
<a name="line59"></a> *
<a name="line60"></a> *   function testSetTimeout() {
<a name="line61"></a> *     var step = 0;
<a name="line62"></a> *     function stepCallback() {
<a name="line63"></a> *       step++;
<a name="line64"></a> *       switch (step) {
<a name="line65"></a> *         case 1:
<a name="line66"></a> *           var startTime = goog.now();
<a name="line67"></a> *           asyncTestCase.waitForAsync(&#39;step 1&#39;);
<a name="line68"></a> *           window.setTimeout(stepCallback, 100);
<a name="line69"></a> *           break;
<a name="line70"></a> *         case 2:
<a name="line71"></a> *           assertTrue(&#39;Timeout fired too soon&#39;,
<a name="line72"></a> *               goog.now() - startTime &gt;= 100);
<a name="line73"></a> *           asyncTestCase.waitForAsync(&#39;step 2&#39;);
<a name="line74"></a> *           window.setTimeout(stepCallback, 100);
<a name="line75"></a> *           break;
<a name="line76"></a> *         case 3:
<a name="line77"></a> *           assertTrue(&#39;Timeout fired too soon&#39;,
<a name="line78"></a> *               goog.now() - startTime &gt;= 200);
<a name="line79"></a> *           asyncTestCase.continueTesting();
<a name="line80"></a> *           break;
<a name="line81"></a> *         default:
<a name="line82"></a> *           fail(&#39;Unexpected call to stepCallback&#39;);
<a name="line83"></a> *       }
<a name="line84"></a> *     }
<a name="line85"></a> *     stepCallback();
<a name="line86"></a> *   }
<a name="line87"></a> *
<a name="line88"></a> * Known Issues:
<a name="line89"></a> *   IE7 Exceptions:
<a name="line90"></a> *     As the failingtest.html will show, it appears as though ie7 does not
<a name="line91"></a> *     propagate an exception past a function called using the func.call()
<a name="line92"></a> *     syntax. This causes case 3 of the failing tests (exceptions) to show up
<a name="line93"></a> *     as timeouts in IE.
<a name="line94"></a> *   window.onerror:
<a name="line95"></a> *     This seems to catch errors only in ff2/ff3. It does not work in Safari or
<a name="line96"></a> *     IE7. The consequence of this is that exceptions that would have been
<a name="line97"></a> *     caught by window.onerror show up as timeouts.
<a name="line98"></a> *
<a name="line99"></a> * @author agrieve@google.com (Andrew Grieve)
<a name="line100"></a> */
<a name="line101"></a>
<a name="line102"></a>goog.provide(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line103"></a>goog.provide(&#39;goog.testing.AsyncTestCase.ControlBreakingException&#39;);
<a name="line104"></a>
<a name="line105"></a>goog.require(&#39;goog.testing.TestCase&#39;);
<a name="line106"></a>goog.require(&#39;goog.testing.TestCase.Test&#39;);
<a name="line107"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * A test case that is capable of running tests the contain asynchronous logic.
<a name="line113"></a> * @param {string=} opt_name A descriptive name for the test case.
<a name="line114"></a> * @extends {goog.testing.TestCase}
<a name="line115"></a> * @constructor
<a name="line116"></a> */
<a name="line117"></a>goog.testing.AsyncTestCase = function(opt_name) {
<a name="line118"></a>  goog.testing.TestCase.call(this, opt_name);
<a name="line119"></a>};
<a name="line120"></a>goog.inherits(goog.testing.AsyncTestCase, goog.testing.TestCase);
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Represents result of top stack function call.
<a name="line125"></a> * @typedef {{controlBreakingExceptionThrown: boolean, message: string}}
<a name="line126"></a> * @private
<a name="line127"></a> */
<a name="line128"></a>goog.testing.AsyncTestCase.TopStackFuncResult_;
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * An exception class used solely for control flow.
<a name="line134"></a> * @param {string=} opt_message Error message.
<a name="line135"></a> * @constructor
<a name="line136"></a> */
<a name="line137"></a>goog.testing.AsyncTestCase.ControlBreakingException = function(opt_message) {
<a name="line138"></a>  /**
<a name="line139"></a>   * The exception message.
<a name="line140"></a>   * @type {string}
<a name="line141"></a>   */
<a name="line142"></a>  this.message = opt_message || &#39;&#39;;
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Return value for .toString().
<a name="line148"></a> * @type {string}
<a name="line149"></a> */
<a name="line150"></a>goog.testing.AsyncTestCase.ControlBreakingException.TO_STRING =
<a name="line151"></a>    &#39;[AsyncTestCase.ControlBreakingException]&#39;;
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Marks this object as a ControlBreakingException
<a name="line156"></a> * @type {boolean}
<a name="line157"></a> */
<a name="line158"></a>goog.testing.AsyncTestCase.ControlBreakingException.prototype.
<a name="line159"></a>    isControlBreakingException = true;
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/** @override */
<a name="line163"></a>goog.testing.AsyncTestCase.ControlBreakingException.prototype.toString =
<a name="line164"></a>    function() {
<a name="line165"></a>  // This shows up in the console when the exception is not caught.
<a name="line166"></a>  return goog.testing.AsyncTestCase.ControlBreakingException.TO_STRING;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * How long to wait for a single step of a test to complete in milliseconds.
<a name="line172"></a> * A step starts when a call to waitForAsync() is made.
<a name="line173"></a> * @type {number}
<a name="line174"></a> */
<a name="line175"></a>goog.testing.AsyncTestCase.prototype.stepTimeout = 1000;
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * How long to wait after a failed test before moving onto the next one.
<a name="line180"></a> * The purpose of this is to allow any pending async callbacks from the failing
<a name="line181"></a> * test to finish up and not cause the next test to fail.
<a name="line182"></a> * @type {number}
<a name="line183"></a> */
<a name="line184"></a>goog.testing.AsyncTestCase.prototype.timeToSleepAfterFailure = 500;
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * Turn on extra logging to help debug failing async. tests.
<a name="line189"></a> * @type {boolean}
<a name="line190"></a> * @private
<a name="line191"></a> */
<a name="line192"></a>goog.testing.AsyncTestCase.prototype.enableDebugLogs_ = false;
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * A reference to the original asserts.js assert_() function.
<a name="line197"></a> * @private
<a name="line198"></a> */
<a name="line199"></a>goog.testing.AsyncTestCase.prototype.origAssert_;
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>/**
<a name="line203"></a> * A reference to the original asserts.js fail() function.
<a name="line204"></a> * @private
<a name="line205"></a> */
<a name="line206"></a>goog.testing.AsyncTestCase.prototype.origFail_;
<a name="line207"></a>
<a name="line208"></a>
<a name="line209"></a>/**
<a name="line210"></a> * A reference to the original window.onerror function.
<a name="line211"></a> * @type {Function|undefined}
<a name="line212"></a> * @private
<a name="line213"></a> */
<a name="line214"></a>goog.testing.AsyncTestCase.prototype.origOnError_;
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * The stage of the test we are currently on.
<a name="line219"></a> * @type {Function|undefined}}
<a name="line220"></a> * @private
<a name="line221"></a> */
<a name="line222"></a>goog.testing.AsyncTestCase.prototype.curStepFunc_;
<a name="line223"></a>
<a name="line224"></a>
<a name="line225"></a>/**
<a name="line226"></a> * The name of the stage of the test we are currently on.
<a name="line227"></a> * @type {string}
<a name="line228"></a> * @private
<a name="line229"></a> */
<a name="line230"></a>goog.testing.AsyncTestCase.prototype.curStepName_ = &#39;&#39;;
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * The stage of the test we should run next.
<a name="line235"></a> * @type {Function|undefined}
<a name="line236"></a> * @private
<a name="line237"></a> */
<a name="line238"></a>goog.testing.AsyncTestCase.prototype.nextStepFunc;
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>/**
<a name="line242"></a> * The name of the stage of the test we should run next.
<a name="line243"></a> * @type {string}
<a name="line244"></a> * @private
<a name="line245"></a> */
<a name="line246"></a>goog.testing.AsyncTestCase.prototype.nextStepName_ = &#39;&#39;;
<a name="line247"></a>
<a name="line248"></a>
<a name="line249"></a>/**
<a name="line250"></a> * The handle to the current setTimeout timer.
<a name="line251"></a> * @type {number}
<a name="line252"></a> * @private
<a name="line253"></a> */
<a name="line254"></a>goog.testing.AsyncTestCase.prototype.timeoutHandle_ = 0;
<a name="line255"></a>
<a name="line256"></a>
<a name="line257"></a>/**
<a name="line258"></a> * Marks if the cleanUp() function has been called for the currently running
<a name="line259"></a> * test.
<a name="line260"></a> * @type {boolean}
<a name="line261"></a> * @private
<a name="line262"></a> */
<a name="line263"></a>goog.testing.AsyncTestCase.prototype.cleanedUp_ = false;
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * The currently active test.
<a name="line268"></a> * @type {goog.testing.TestCase.Test|undefined}
<a name="line269"></a> * @protected
<a name="line270"></a> */
<a name="line271"></a>goog.testing.AsyncTestCase.prototype.activeTest;
<a name="line272"></a>
<a name="line273"></a>
<a name="line274"></a>/**
<a name="line275"></a> * A flag to prevent recursive exception handling.
<a name="line276"></a> * @type {boolean}
<a name="line277"></a> * @private
<a name="line278"></a> */
<a name="line279"></a>goog.testing.AsyncTestCase.prototype.inException_ = false;
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Flag used to determine if we can move to the next step in the testing loop.
<a name="line284"></a> * @type {boolean}
<a name="line285"></a> * @private
<a name="line286"></a> */
<a name="line287"></a>goog.testing.AsyncTestCase.prototype.isReady_ = true;
<a name="line288"></a>
<a name="line289"></a>
<a name="line290"></a>/**
<a name="line291"></a> * Flag that tells us if there is a function in the call stack that will make
<a name="line292"></a> * a call to pump_().
<a name="line293"></a> * @type {boolean}
<a name="line294"></a> * @private
<a name="line295"></a> */
<a name="line296"></a>goog.testing.AsyncTestCase.prototype.returnWillPump_ = false;
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>/**
<a name="line300"></a> * The number of times we have thrown a ControlBreakingException so that we
<a name="line301"></a> * know not to complain in our window.onerror handler. In Webkit, window.onerror
<a name="line302"></a> * is not supported, and so this counter will keep going up but we won&#39;t care
<a name="line303"></a> * about it.
<a name="line304"></a> * @type {number}
<a name="line305"></a> * @private
<a name="line306"></a> */
<a name="line307"></a>goog.testing.AsyncTestCase.prototype.numControlExceptionsExpected_ = 0;
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>/**
<a name="line311"></a> * The current step name.
<a name="line312"></a> * @return {!string} Step name.
<a name="line313"></a> * @protected
<a name="line314"></a> */
<a name="line315"></a>goog.testing.AsyncTestCase.prototype.getCurrentStepName = function() {
<a name="line316"></a>  return this.curStepName_;
<a name="line317"></a>};
<a name="line318"></a>
<a name="line319"></a>
<a name="line320"></a>/**
<a name="line321"></a> * Preferred way of creating an AsyncTestCase. Creates one and initializes it
<a name="line322"></a> * with the G_testRunner.
<a name="line323"></a> * @param {string=} opt_name A descriptive name for the test case.
<a name="line324"></a> * @return {goog.testing.AsyncTestCase} The created AsyncTestCase.
<a name="line325"></a> */
<a name="line326"></a>goog.testing.AsyncTestCase.createAndInstall = function(opt_name) {
<a name="line327"></a>  var asyncTestCase = new goog.testing.AsyncTestCase(opt_name);
<a name="line328"></a>  goog.testing.TestCase.initializeTestRunner(asyncTestCase);
<a name="line329"></a>  return asyncTestCase;
<a name="line330"></a>};
<a name="line331"></a>
<a name="line332"></a>
<a name="line333"></a>/**
<a name="line334"></a> * Informs the testcase not to continue to the next step in the test cycle
<a name="line335"></a> * until continueTesting is called.
<a name="line336"></a> * @param {string=} opt_name A description of what we are waiting for.
<a name="line337"></a> */
<a name="line338"></a>goog.testing.AsyncTestCase.prototype.waitForAsync = function(opt_name) {
<a name="line339"></a>  this.isReady_ = false;
<a name="line340"></a>  this.curStepName_ = opt_name || this.curStepName_;
<a name="line341"></a>
<a name="line342"></a>  // Reset the timer that tracks if the async test takes too long.
<a name="line343"></a>  this.stopTimeoutTimer_();
<a name="line344"></a>  this.startTimeoutTimer_();
<a name="line345"></a>};
<a name="line346"></a>
<a name="line347"></a>
<a name="line348"></a>/**
<a name="line349"></a> * Continue with the next step in the test cycle.
<a name="line350"></a> */
<a name="line351"></a>goog.testing.AsyncTestCase.prototype.continueTesting = function() {
<a name="line352"></a>  if (!this.isReady_) {
<a name="line353"></a>    // We are a potential entry point, so we pump.
<a name="line354"></a>    this.isReady_ = true;
<a name="line355"></a>    this.stopTimeoutTimer_();
<a name="line356"></a>    // Run this in a setTimeout so that the caller has a chance to call
<a name="line357"></a>    // waitForAsync() again before we continue.
<a name="line358"></a>    this.timeout(goog.bind(this.pump_, this, null), 0);
<a name="line359"></a>  }
<a name="line360"></a>};
<a name="line361"></a>
<a name="line362"></a>
<a name="line363"></a>/**
<a name="line364"></a> * Handles an exception thrown by a test.
<a name="line365"></a> * @param {*=} opt_e The exception object associated with the failure
<a name="line366"></a> *     or a string.
<a name="line367"></a> * @throws Always throws a ControlBreakingException.
<a name="line368"></a> */
<a name="line369"></a>goog.testing.AsyncTestCase.prototype.doAsyncError = function(opt_e) {
<a name="line370"></a>  // If we&#39;ve caught an exception that we threw, then just pass it along. This
<a name="line371"></a>  // can happen if doAsyncError() was called from a call to assert and then
<a name="line372"></a>  // again by pump_().
<a name="line373"></a>  if (opt_e &amp;&amp; opt_e.isControlBreakingException) {
<a name="line374"></a>    throw opt_e;
<a name="line375"></a>  }
<a name="line376"></a>
<a name="line377"></a>  // Prevent another timeout error from triggering for this test step.
<a name="line378"></a>  this.stopTimeoutTimer_();
<a name="line379"></a>
<a name="line380"></a>  // doError() uses test.name. Here, we create a dummy test and give it a more
<a name="line381"></a>  // helpful name based on the step we&#39;re currently on.
<a name="line382"></a>  var fakeTestObj = new goog.testing.TestCase.Test(this.curStepName_,
<a name="line383"></a>                                                   goog.nullFunction);
<a name="line384"></a>  if (this.activeTest) {
<a name="line385"></a>    fakeTestObj.name = this.activeTest.name + &#39; [&#39; + fakeTestObj.name + &#39;]&#39;;
<a name="line386"></a>  }
<a name="line387"></a>
<a name="line388"></a>  if (this.activeTest) {
<a name="line389"></a>    // Note: if the test has an error, and then tearDown has an error, they will
<a name="line390"></a>    // both be reported.
<a name="line391"></a>    this.doError(fakeTestObj, opt_e);
<a name="line392"></a>  } else {
<a name="line393"></a>    this.exceptionBeforeTest = opt_e;
<a name="line394"></a>  }
<a name="line395"></a>
<a name="line396"></a>  // This is a potential entry point, so we pump. We also add in a bit of a
<a name="line397"></a>  // delay to try and prevent any async behavior from the failed test from
<a name="line398"></a>  // causing the next test to fail.
<a name="line399"></a>  this.timeout(goog.bind(this.pump_, this, this.doAsyncErrorTearDown_),
<a name="line400"></a>      this.timeToSleepAfterFailure);
<a name="line401"></a>
<a name="line402"></a>  // We just caught an exception, so we do not want the code above us on the
<a name="line403"></a>  // stack to continue executing. If pump_ is in our call-stack, then it will
<a name="line404"></a>  // batch together multiple errors, so we only increment the count if pump_ is
<a name="line405"></a>  // not in the stack and let pump_ increment the count when it batches them.
<a name="line406"></a>  if (!this.returnWillPump_) {
<a name="line407"></a>    this.numControlExceptionsExpected_ += 1;
<a name="line408"></a>    this.dbgLog_(&#39;doAsynError: numControlExceptionsExpected_ = &#39; +
<a name="line409"></a>        this.numControlExceptionsExpected_ + &#39; and throwing exception.&#39;);
<a name="line410"></a>  }
<a name="line411"></a>
<a name="line412"></a>  // Copy the error message to ControlBreakingException.
<a name="line413"></a>  var message = &#39;&#39;;
<a name="line414"></a>  if (typeof opt_e == &#39;string&#39;) {
<a name="line415"></a>    message = opt_e;
<a name="line416"></a>  } else if (opt_e &amp;&amp; opt_e.message) {
<a name="line417"></a>    message = opt_e.message;
<a name="line418"></a>  }
<a name="line419"></a>  throw new goog.testing.AsyncTestCase.ControlBreakingException(message);
<a name="line420"></a>};
<a name="line421"></a>
<a name="line422"></a>
<a name="line423"></a>/**
<a name="line424"></a> * Sets up the test page and then waits until the test case has been marked
<a name="line425"></a> * as ready before executing the tests.
<a name="line426"></a> * @override
<a name="line427"></a> */
<a name="line428"></a>goog.testing.AsyncTestCase.prototype.runTests = function() {
<a name="line429"></a>  this.hookAssert_();
<a name="line430"></a>  this.hookOnError_();
<a name="line431"></a>
<a name="line432"></a>  this.setNextStep_(this.doSetUpPage_, &#39;setUpPage&#39;);
<a name="line433"></a>  // We are an entry point, so we pump.
<a name="line434"></a>  this.pump_();
<a name="line435"></a>};
<a name="line436"></a>
<a name="line437"></a>
<a name="line438"></a>/**
<a name="line439"></a> * Starts the tests.
<a name="line440"></a> * @override
<a name="line441"></a> */
<a name="line442"></a>goog.testing.AsyncTestCase.prototype.cycleTests = function() {
<a name="line443"></a>  // We are an entry point, so we pump.
<a name="line444"></a>  this.saveMessage(&#39;Start&#39;);
<a name="line445"></a>  this.setNextStep_(this.doIteration_, &#39;doIteration&#39;);
<a name="line446"></a>  this.pump_();
<a name="line447"></a>};
<a name="line448"></a>
<a name="line449"></a>
<a name="line450"></a>/**
<a name="line451"></a> * Finalizes the test case, called when the tests have finished executing.
<a name="line452"></a> * @override
<a name="line453"></a> */
<a name="line454"></a>goog.testing.AsyncTestCase.prototype.finalize = function() {
<a name="line455"></a>  this.unhookAll_();
<a name="line456"></a>  this.setNextStep_(null, &#39;finalized&#39;);
<a name="line457"></a>  goog.testing.AsyncTestCase.superClass_.finalize.call(this);
<a name="line458"></a>};
<a name="line459"></a>
<a name="line460"></a>
<a name="line461"></a>/**
<a name="line462"></a> * Enables verbose logging of what is happening inside of the AsyncTestCase.
<a name="line463"></a> */
<a name="line464"></a>goog.testing.AsyncTestCase.prototype.enableDebugLogging = function() {
<a name="line465"></a>  this.enableDebugLogs_ = true;
<a name="line466"></a>};
<a name="line467"></a>
<a name="line468"></a>
<a name="line469"></a>/**
<a name="line470"></a> * Logs the given debug message to the console (when enabled).
<a name="line471"></a> * @param {string} message The message to log.
<a name="line472"></a> * @private
<a name="line473"></a> */
<a name="line474"></a>goog.testing.AsyncTestCase.prototype.dbgLog_ = function(message) {
<a name="line475"></a>  if (this.enableDebugLogs_) {
<a name="line476"></a>    this.log(&#39;AsyncTestCase - &#39; + message);
<a name="line477"></a>  }
<a name="line478"></a>};
<a name="line479"></a>
<a name="line480"></a>
<a name="line481"></a>/**
<a name="line482"></a> * Wraps doAsyncError() for when we are sure that the test runner has no user
<a name="line483"></a> * code above it in the stack.
<a name="line484"></a> * @param {string|Error=} opt_e The exception object associated with the
<a name="line485"></a> *     failure or a string.
<a name="line486"></a> * @private
<a name="line487"></a> */
<a name="line488"></a>goog.testing.AsyncTestCase.prototype.doTopOfStackAsyncError_ =
<a name="line489"></a>    function(opt_e) {
<a name="line490"></a>  /** @preserveTry */
<a name="line491"></a>  try {
<a name="line492"></a>    this.doAsyncError(opt_e);
<a name="line493"></a>  } catch (e) {
<a name="line494"></a>    // We know that we are on the top of the stack, so there is no need to
<a name="line495"></a>    // throw this exception in this case.
<a name="line496"></a>    if (e.isControlBreakingException) {
<a name="line497"></a>      this.numControlExceptionsExpected_ -= 1;
<a name="line498"></a>      this.dbgLog_(&#39;doTopOfStackAsyncError_: numControlExceptionsExpected_ = &#39; +
<a name="line499"></a>          this.numControlExceptionsExpected_ + &#39; and catching exception.&#39;);
<a name="line500"></a>    } else {
<a name="line501"></a>      throw e;
<a name="line502"></a>    }
<a name="line503"></a>  }
<a name="line504"></a>};
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>/**
<a name="line508"></a> * Calls the tearDown function, catching any errors, and then moves on to
<a name="line509"></a> * the next step in the testing cycle.
<a name="line510"></a> * @private
<a name="line511"></a> */
<a name="line512"></a>goog.testing.AsyncTestCase.prototype.doAsyncErrorTearDown_ = function() {
<a name="line513"></a>  if (this.inException_) {
<a name="line514"></a>    // We get here if tearDown is throwing the error.
<a name="line515"></a>    // Upon calling continueTesting, the inline function &#39;doAsyncError&#39; (set
<a name="line516"></a>    // below) is run.
<a name="line517"></a>    this.continueTesting();
<a name="line518"></a>  } else {
<a name="line519"></a>    this.inException_ = true;
<a name="line520"></a>    this.isReady_ = true;
<a name="line521"></a>
<a name="line522"></a>    // The continue point is different depending on if the error happened in
<a name="line523"></a>    // setUpPage() or in setUp()/test*()/tearDown().
<a name="line524"></a>    var stepFuncAfterError = this.nextStepFunc_;
<a name="line525"></a>    var stepNameAfterError = &#39;TestCase.execute (after error)&#39;;
<a name="line526"></a>    if (this.activeTest) {
<a name="line527"></a>      stepFuncAfterError = this.doIteration_;
<a name="line528"></a>      stepNameAfterError = &#39;doIteration (after error)&#39;;
<a name="line529"></a>    }
<a name="line530"></a>
<a name="line531"></a>    // We must set the next step before calling tearDown.
<a name="line532"></a>    this.setNextStep_(function() {
<a name="line533"></a>      this.inException_ = false;
<a name="line534"></a>      // This is null when an error happens in setUpPage.
<a name="line535"></a>      this.setNextStep_(stepFuncAfterError, stepNameAfterError);
<a name="line536"></a>    }, &#39;doAsyncError&#39;);
<a name="line537"></a>
<a name="line538"></a>    // Call the test&#39;s tearDown().
<a name="line539"></a>    if (!this.cleanedUp_) {
<a name="line540"></a>      this.cleanedUp_ = true;
<a name="line541"></a>      this.tearDown();
<a name="line542"></a>    }
<a name="line543"></a>  }
<a name="line544"></a>};
<a name="line545"></a>
<a name="line546"></a>
<a name="line547"></a>/**
<a name="line548"></a> * Replaces the asserts.js assert_() and fail() functions with a wrappers to
<a name="line549"></a> * catch the exceptions.
<a name="line550"></a> * @private
<a name="line551"></a> */
<a name="line552"></a>goog.testing.AsyncTestCase.prototype.hookAssert_ = function() {
<a name="line553"></a>  if (!this.origAssert_) {
<a name="line554"></a>    this.origAssert_ = _assert;
<a name="line555"></a>    this.origFail_ = fail;
<a name="line556"></a>    var self = this;
<a name="line557"></a>    _assert = function() {
<a name="line558"></a>      /** @preserveTry */
<a name="line559"></a>      try {
<a name="line560"></a>        self.origAssert_.apply(this, arguments);
<a name="line561"></a>      } catch (e) {
<a name="line562"></a>        self.dbgLog_(&#39;Wrapping failed assert()&#39;);
<a name="line563"></a>        self.doAsyncError(e);
<a name="line564"></a>      }
<a name="line565"></a>    };
<a name="line566"></a>    fail = function() {
<a name="line567"></a>      /** @preserveTry */
<a name="line568"></a>      try {
<a name="line569"></a>        self.origFail_.apply(this, arguments);
<a name="line570"></a>      } catch (e) {
<a name="line571"></a>        self.dbgLog_(&#39;Wrapping fail()&#39;);
<a name="line572"></a>        self.doAsyncError(e);
<a name="line573"></a>      }
<a name="line574"></a>    };
<a name="line575"></a>  }
<a name="line576"></a>};
<a name="line577"></a>
<a name="line578"></a>
<a name="line579"></a>/**
<a name="line580"></a> * Sets a window.onerror handler for catching exceptions that happen in async
<a name="line581"></a> * callbacks. Note that as of Safari 3.1, Safari does not support this.
<a name="line582"></a> * @private
<a name="line583"></a> */
<a name="line584"></a>goog.testing.AsyncTestCase.prototype.hookOnError_ = function() {
<a name="line585"></a>  if (!this.origOnError_) {
<a name="line586"></a>    this.origOnError_ = window.onerror;
<a name="line587"></a>    var self = this;
<a name="line588"></a>    window.onerror = function(error, url, line) {
<a name="line589"></a>      // Ignore exceptions that we threw on purpose.
<a name="line590"></a>      var cbe =
<a name="line591"></a>          goog.testing.AsyncTestCase.ControlBreakingException.TO_STRING;
<a name="line592"></a>      if (String(error).indexOf(cbe) != -1 &amp;&amp;
<a name="line593"></a>          self.numControlExceptionsExpected_) {
<a name="line594"></a>        self.numControlExceptionsExpected_ -= 1;
<a name="line595"></a>        self.dbgLog_(&#39;window.onerror: numControlExceptionsExpected_ = &#39; +
<a name="line596"></a>            self.numControlExceptionsExpected_ + &#39; and ignoring exception. &#39; +
<a name="line597"></a>            error);
<a name="line598"></a>        // Tell the browser not to compain about the error.
<a name="line599"></a>        return true;
<a name="line600"></a>      } else {
<a name="line601"></a>        self.dbgLog_(&#39;window.onerror caught exception.&#39;);
<a name="line602"></a>        var message = error + &#39;\nURL: &#39; + url + &#39;\nLine: &#39; + line;
<a name="line603"></a>        self.doTopOfStackAsyncError_(message);
<a name="line604"></a>        // Tell the browser to complain about the error.
<a name="line605"></a>        return false;
<a name="line606"></a>      }
<a name="line607"></a>    };
<a name="line608"></a>  }
<a name="line609"></a>};
<a name="line610"></a>
<a name="line611"></a>
<a name="line612"></a>/**
<a name="line613"></a> * Unhooks window.onerror and _assert.
<a name="line614"></a> * @private
<a name="line615"></a> */
<a name="line616"></a>goog.testing.AsyncTestCase.prototype.unhookAll_ = function() {
<a name="line617"></a>  if (this.origOnError_) {
<a name="line618"></a>    window.onerror = this.origOnError_;
<a name="line619"></a>    this.origOnError_ = null;
<a name="line620"></a>    _assert = this.origAssert_;
<a name="line621"></a>    this.origAssert_ = null;
<a name="line622"></a>    fail = this.origFail_;
<a name="line623"></a>    this.origFail_ = null;
<a name="line624"></a>  }
<a name="line625"></a>};
<a name="line626"></a>
<a name="line627"></a>
<a name="line628"></a>/**
<a name="line629"></a> * Enables the timeout timer. This timer fires unless continueTesting is
<a name="line630"></a> * called.
<a name="line631"></a> * @private
<a name="line632"></a> */
<a name="line633"></a>goog.testing.AsyncTestCase.prototype.startTimeoutTimer_ = function() {
<a name="line634"></a>  if (!this.timeoutHandle_ &amp;&amp; this.stepTimeout &gt; 0) {
<a name="line635"></a>    this.timeoutHandle_ = this.timeout(goog.bind(function() {
<a name="line636"></a>      this.dbgLog_(&#39;Timeout timer fired with id &#39; + this.timeoutHandle_);
<a name="line637"></a>      this.timeoutHandle_ = 0;
<a name="line638"></a>
<a name="line639"></a>      this.doTopOfStackAsyncError_(&#39;Timed out while waiting for &#39; +
<a name="line640"></a>          &#39;continueTesting() to be called.&#39;);
<a name="line641"></a>    }, this, null), this.stepTimeout);
<a name="line642"></a>    this.dbgLog_(&#39;Started timeout timer with id &#39; + this.timeoutHandle_);
<a name="line643"></a>  }
<a name="line644"></a>};
<a name="line645"></a>
<a name="line646"></a>
<a name="line647"></a>/**
<a name="line648"></a> * Disables the timeout timer.
<a name="line649"></a> * @private
<a name="line650"></a> */
<a name="line651"></a>goog.testing.AsyncTestCase.prototype.stopTimeoutTimer_ = function() {
<a name="line652"></a>  if (this.timeoutHandle_) {
<a name="line653"></a>    this.dbgLog_(&#39;Clearing timeout timer with id &#39; + this.timeoutHandle_);
<a name="line654"></a>    this.clearTimeout(this.timeoutHandle_);
<a name="line655"></a>    this.timeoutHandle_ = 0;
<a name="line656"></a>  }
<a name="line657"></a>};
<a name="line658"></a>
<a name="line659"></a>
<a name="line660"></a>/**
<a name="line661"></a> * Sets the next function to call in our sequence of async callbacks.
<a name="line662"></a> * @param {Function} func The function that executes the next step.
<a name="line663"></a> * @param {string} name A description of the next step.
<a name="line664"></a> * @private
<a name="line665"></a> */
<a name="line666"></a>goog.testing.AsyncTestCase.prototype.setNextStep_ = function(func, name) {
<a name="line667"></a>  this.nextStepFunc_ = func &amp;&amp; goog.bind(func, this);
<a name="line668"></a>  this.nextStepName_ = name;
<a name="line669"></a>};
<a name="line670"></a>
<a name="line671"></a>
<a name="line672"></a>/**
<a name="line673"></a> * Calls the given function, redirecting any exceptions to doAsyncError.
<a name="line674"></a> * @param {Function} func The function to call.
<a name="line675"></a> * @return {!goog.testing.AsyncTestCase.TopStackFuncResult_} Returns a
<a name="line676"></a> * TopStackFuncResult_.
<a name="line677"></a> * @private
<a name="line678"></a> */
<a name="line679"></a>goog.testing.AsyncTestCase.prototype.callTopOfStackFunc_ = function(func) {
<a name="line680"></a>  /** @preserveTry */
<a name="line681"></a>  try {
<a name="line682"></a>    func.call(this);
<a name="line683"></a>    return {controlBreakingExceptionThrown: false, message: &#39;&#39;};
<a name="line684"></a>  } catch (e) {
<a name="line685"></a>    this.dbgLog_(&#39;Caught exception in callTopOfStackFunc_&#39;);
<a name="line686"></a>    /** @preserveTry */
<a name="line687"></a>    try {
<a name="line688"></a>      this.doAsyncError(e);
<a name="line689"></a>      return {controlBreakingExceptionThrown: false, message: &#39;&#39;};
<a name="line690"></a>    } catch (e2) {
<a name="line691"></a>      if (!e2.isControlBreakingException) {
<a name="line692"></a>        throw e2;
<a name="line693"></a>      }
<a name="line694"></a>      return {controlBreakingExceptionThrown: true, message: e2.message};
<a name="line695"></a>    }
<a name="line696"></a>  }
<a name="line697"></a>};
<a name="line698"></a>
<a name="line699"></a>
<a name="line700"></a>/**
<a name="line701"></a> * Calls the next callback when the isReady_ flag is true.
<a name="line702"></a> * @param {Function=} opt_doFirst A function to call before pumping.
<a name="line703"></a> * @private
<a name="line704"></a> * @throws Throws a ControlBreakingException if there were any failing steps.
<a name="line705"></a> */
<a name="line706"></a>goog.testing.AsyncTestCase.prototype.pump_ = function(opt_doFirst) {
<a name="line707"></a>  // If this function is already above us in the call-stack, then we should
<a name="line708"></a>  // return rather than pumping in order to minimize call-stack depth.
<a name="line709"></a>  if (!this.returnWillPump_) {
<a name="line710"></a>    this.setBatchTime(this.now());
<a name="line711"></a>    this.returnWillPump_ = true;
<a name="line712"></a>    var topFuncResult = {};
<a name="line713"></a>
<a name="line714"></a>    if (opt_doFirst) {
<a name="line715"></a>      topFuncResult = this.callTopOfStackFunc_(opt_doFirst);
<a name="line716"></a>    }
<a name="line717"></a>    // Note: we don&#39;t check for this.running here because it is not set to true
<a name="line718"></a>    // while executing setUpPage and tearDownPage.
<a name="line719"></a>    // Also, if isReady_ is false, then one of two things will happen:
<a name="line720"></a>    // 1. Our timeout callback will be called.
<a name="line721"></a>    // 2. The tests will call continueTesting(), which will call pump_() again.
<a name="line722"></a>    while (this.isReady_ &amp;&amp; this.nextStepFunc_ &amp;&amp;
<a name="line723"></a>        !topFuncResult.controlBreakingExceptionThrown) {
<a name="line724"></a>      this.curStepFunc_ = this.nextStepFunc_;
<a name="line725"></a>      this.curStepName_ = this.nextStepName_;
<a name="line726"></a>      this.nextStepFunc_ = null;
<a name="line727"></a>      this.nextStepName_ = &#39;&#39;;
<a name="line728"></a>
<a name="line729"></a>      this.dbgLog_(&#39;Performing step: &#39; + this.curStepName_);
<a name="line730"></a>      topFuncResult =
<a name="line731"></a>          this.callTopOfStackFunc_(/** @type {Function} */(this.curStepFunc_));
<a name="line732"></a>
<a name="line733"></a>      // If the max run time is exceeded call this function again async so as
<a name="line734"></a>      // not to block the browser.
<a name="line735"></a>      var delta = this.now() - this.getBatchTime();
<a name="line736"></a>      if (delta &gt; goog.testing.TestCase.MAX_RUN_TIME &amp;&amp;
<a name="line737"></a>          !topFuncResult.controlBreakingExceptionThrown) {
<a name="line738"></a>        this.saveMessage(&#39;Breaking async&#39;);
<a name="line739"></a>        var self = this;
<a name="line740"></a>        this.timeout(function() { self.pump_(); }, 100);
<a name="line741"></a>        break;
<a name="line742"></a>      }
<a name="line743"></a>    }
<a name="line744"></a>    this.returnWillPump_ = false;
<a name="line745"></a>  } else if (opt_doFirst) {
<a name="line746"></a>    opt_doFirst.call(this);
<a name="line747"></a>  }
<a name="line748"></a>};
<a name="line749"></a>
<a name="line750"></a>
<a name="line751"></a>/**
<a name="line752"></a> * Sets up the test page and then waits untill the test case has been marked
<a name="line753"></a> * as ready before executing the tests.
<a name="line754"></a> * @private
<a name="line755"></a> */
<a name="line756"></a>goog.testing.AsyncTestCase.prototype.doSetUpPage_ = function() {
<a name="line757"></a>  this.setNextStep_(this.execute, &#39;TestCase.execute&#39;);
<a name="line758"></a>  this.setUpPage();
<a name="line759"></a>};
<a name="line760"></a>
<a name="line761"></a>
<a name="line762"></a>/**
<a name="line763"></a> * Step 1: Move to the next test.
<a name="line764"></a> * @private
<a name="line765"></a> */
<a name="line766"></a>goog.testing.AsyncTestCase.prototype.doIteration_ = function() {
<a name="line767"></a>  this.activeTest = this.next();
<a name="line768"></a>  if (this.activeTest &amp;&amp; this.running) {
<a name="line769"></a>    this.result_.runCount++;
<a name="line770"></a>    // If this test should be marked as having failed, doIteration will go
<a name="line771"></a>    // straight to the next test.
<a name="line772"></a>    if (this.maybeFailTestEarly(this.activeTest)) {
<a name="line773"></a>      this.setNextStep_(this.doIteration_, &#39;doIteration&#39;);
<a name="line774"></a>    } else {
<a name="line775"></a>      this.setNextStep_(this.doSetUp_, &#39;setUp&#39;);
<a name="line776"></a>    }
<a name="line777"></a>  } else {
<a name="line778"></a>    // All tests done.
<a name="line779"></a>    this.finalize();
<a name="line780"></a>  }
<a name="line781"></a>};
<a name="line782"></a>
<a name="line783"></a>
<a name="line784"></a>/**
<a name="line785"></a> * Step 2: Call setUp().
<a name="line786"></a> * @private
<a name="line787"></a> */
<a name="line788"></a>goog.testing.AsyncTestCase.prototype.doSetUp_ = function() {
<a name="line789"></a>  this.log(&#39;Running test: &#39; + this.activeTest.name);
<a name="line790"></a>  this.cleanedUp_ = false;
<a name="line791"></a>  this.setNextStep_(this.doExecute_, this.activeTest.name);
<a name="line792"></a>  this.setUp();
<a name="line793"></a>};
<a name="line794"></a>
<a name="line795"></a>
<a name="line796"></a>/**
<a name="line797"></a> * Step 3: Call test.execute().
<a name="line798"></a> * @private
<a name="line799"></a> */
<a name="line800"></a>goog.testing.AsyncTestCase.prototype.doExecute_ = function() {
<a name="line801"></a>  this.setNextStep_(this.doTearDown_, &#39;tearDown&#39;);
<a name="line802"></a>  this.activeTest.execute();
<a name="line803"></a>};
<a name="line804"></a>
<a name="line805"></a>
<a name="line806"></a>/**
<a name="line807"></a> * Step 4: Call tearDown().
<a name="line808"></a> * @private
<a name="line809"></a> */
<a name="line810"></a>goog.testing.AsyncTestCase.prototype.doTearDown_ = function() {
<a name="line811"></a>  this.cleanedUp_ = true;
<a name="line812"></a>  this.setNextStep_(this.doNext_, &#39;doNext&#39;);
<a name="line813"></a>  this.tearDown();
<a name="line814"></a>};
<a name="line815"></a>
<a name="line816"></a>
<a name="line817"></a>/**
<a name="line818"></a> * Step 5: Call doSuccess()
<a name="line819"></a> * @private
<a name="line820"></a> */
<a name="line821"></a>goog.testing.AsyncTestCase.prototype.doNext_ = function() {
<a name="line822"></a>  this.setNextStep_(this.doIteration_, &#39;doIteration&#39;);
<a name="line823"></a>  this.doSuccess(/** @type {goog.testing.TestCase.Test} */(this.activeTest));
<a name="line824"></a>};
</pre>


</body>
</html>
